; Macro file for Duet WiFi
; Generated by Modix - Version 3.6.3 Macro A

var probing_tool = 0.000
; This allows you to compensate for whatever tool you use to measure the nozzle distance.
; It is set to 0.000 by default, you can set this to the thickness of the tool or paper you use for the Z-offset calibration
; A sheet of paper is on average 0.05 to 0.10mm thick.

M300 S666 P666              							; beep
M291 S3 R"Setting the Z-offset Height" P"Press OK to continue, or CANCEL to abort"

G10 P1 Z0												; set secondary Z-offset to Z=0
echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z0"		; Set the Z-offset to 0 before starting

G29 S2                      							; cancel mesh bed compensation
G28														; home all
G29 S2                      							; cancel mesh bed compensation

T-1														; deselect any tools
G4 P0													; wait for the movement to stop
G28 Y
G4 P0													; wait for the movement to stop
T1 														; Select T1
G4 P0													; wait for the movement to stop
M116 P1 S5

G29 S2													; cancel mesh bed compensation
M290 R0 S0												; cancel baby stepping

G90														; absolute movements
G1 Z10													; move the nozzle up to Z=10
G1 X100 Y100 F5000										; move to X100, Y100
G4 P0													; wait for the movement to stop

M300 S1111 P666											; beep
M564 H1 S0												; ignore axis limits
M291 S2 R"Test Z Probe Trigger Height - Step 1" P"Place a paper sheet under the nozzle and raise the bed until slight friction can be noticed" Z1 ;

G92 Z0													; the nozzle should now be just touching the bed so set the logical Z position to match the physical Z position
M300 S666 P666											; beep
G1 Z8 F300												; insure Z position will allow for probing
T-1 P0
G28 U
T0 P0													; select T0 so you can use the BLTouch
G1 Y{100-sensors.probes[0].offsets[1]} F5000 			; move nozzle so that BLTouch is in it's position 
G90														; absolute moves
G4 P0													; wait for the movement to stop

M291 S2 R"Test Z Probe Trigger Height - Step 2" P"Please remove the paper sheet and wait for the bed-probing to begin" ;
G30 S-1													; probe the bed and report the Z probe trigger height
M564 S1 H1												; respect axis limits

var Z_auto_offset_temp = {sensors.probes[0].lastStopHeight+var.probing_tool}  					; Adjust for the thickness of the probing tool
var Z_auto_offset_secondary = {var.Z_auto_offset_temp - sensors.probes[0].triggerHeight}		; set the offset so it's in relation to the primary nozzle
echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z"^{var.Z_auto_offset_secondary}				; write the Z-offset to the config_probe.g file

echo "the Z-offset has been stored in the config_probe_secondary.g file. The Z-offset secondary is", var.Z_auto_offset_secondary, "mm"

G4 P0						; wait for the movement to stop
G91							; relative movements
G1 Z10 F300					; Lift up the nozzle again so it's not pressed against the bed surface
G90							; absolute movements
T-1 P0						; deselect all tools
M18							; release stepper motors